/*
 * 
 *
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MatchFrame.java
 *
 * Created on 28/08/2009, 15:13:02
 */

package Xadrez.View;


import java.awt.Graphics;
import java.awt.event.ActionListener;
import Xadrez.Modelo.*;
import java.awt.Point;
/**
 *
 * @author Jéssica
 */
public class MatchFrame extends javax.swing.JFrame {
public TabuleiroGrafico tab;
public Match proxy;
    /** Creates new form MatchFrame */
    public MatchFrame() {
        initComponents();
        tab=new TabuleiroGrafico();
        proxy=null;
    }

    public void AdicionarOuvinte(ActionListener ouvinte){
        //this.jButtonAdicionar.addActionListener(ouvinte);
        this.jButtonMover.addActionListener(ouvinte);
        this.jButtonShow.addActionListener(ouvinte);
        this.jButtonUndo.addActionListener(ouvinte);

    }

    /*public javax.swing.JButton getButtonAdicionar(){
    return this.jButtonAdicionar;
    }*/

    public javax.swing.JButton getButton1(){
        return this.jButtonMover;
    }

    public javax.swing.JButton getButtonShow(){
        return this.jButtonShow;
    }

    public javax.swing.JButton getButtonUndo(){
        return this.jButtonUndo;
    }

    public javax.swing.JRadioButton getRadio1(){
        return this.jRadioButton1;
    }

    public javax.swing.JRadioButton getRadio2(){
        return this.jRadioButton2;
    }





    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jTextField5 = new javax.swing.JTextField();
        jTextField6 = new javax.swing.JTextField();
        jTextField7 = new javax.swing.JTextField();
        jButtonMover = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButtonShow = new javax.swing.JButton();
        jButtonUndo = new javax.swing.JButton();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jLabel3 = new javax.swing.JLabel();
        GraphicPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        jLabel4.setText("Mover peça");

        jButtonMover.setText("Mover");

        jLabel1.setText("De");

        jLabel2.setText("Para");

        jButtonShow.setText("Mostrar opçoes");

        jButtonUndo.setText("Desfazer");
        jButtonUndo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonUndoActionPerformed(evt);
            }
        });

        jRadioButton1.setText("Preto");

        jRadioButton2.setText("Branco");

        jLabel3.setText("jLabel3");

        GraphicPanel.setBorder(new javax.swing.border.MatteBorder(null));

        javax.swing.GroupLayout GraphicPanelLayout = new javax.swing.GroupLayout(GraphicPanel);
        GraphicPanel.setLayout(GraphicPanelLayout);
        GraphicPanelLayout.setHorizontalGroup(
            GraphicPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 386, Short.MAX_VALUE)
        );
        GraphicPanelLayout.setVerticalGroup(
            GraphicPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 538, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(GraphicPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(jTextField6, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jTextField4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jTextField7, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jTextField5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jButtonMover))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButtonUndo)
                            .addComponent(jButtonShow)))
                    .addComponent(jRadioButton1)
                    .addComponent(jRadioButton2)
                    .addComponent(jLabel3))
                .addContainerGap(21, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(GraphicPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)
                            .addComponent(jButtonShow))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButtonMover)
                            .addComponent(jButtonUndo))
                        .addGap(29, 29, 29)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton2)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        // TODO add your handling code here:
        Point p=(Point) evt.getPoint().clone();
        Paint(proxy);
           p.y-=(GraphicPanel.getBounds().height/16);

        System.out.println(String.valueOf(GraphicPanel.getBounds().height/8)+" vai clicar na tua mãe!"+p);
        if (GraphicPanel.getBounds().contains(p))
        {
           p.x-=(int)GraphicPanel.getBounds().getX();
           p.y-=(int)GraphicPanel.getBounds().getY();

            System.out.println(p);
            p=tab.toGameSpace(p);

         

            if (proxy!=null)
            {

 
                if (tab.State==TabuleiroGrafico.PlayState.CHOOSING_PIECE_PLAYER)
               {
                    tab.Click(p.x, p.y, proxy);
                this.jTextField4.setText(String.valueOf(p.x));
                this.jTextField5.setText(String.valueOf(p.y));
                this.jTextField6.setText("");
                this.jTextField7.setText("");

                this.jButtonShow.doClick();
                System.out.println("do show");
                }
                else
                 if (tab.State==TabuleiroGrafico.PlayState.CHOOSING_DESTINATION_PLAYER)
                 {
                      tab.Click(p.x, p.y, proxy);
                       if (tab.State!=TabuleiroGrafico.PlayState.CHOOSING_DESTINATION_PLAYER)
                      {
                        this.jTextField6.setText(String.valueOf(p.x));
                        this.jTextField7.setText(String.valueOf(p.y));
                        this.jButtonMover.doClick();
                      }
                       else
                       {
                                       this.jTextField4.setText(String.valueOf(p.x));
                this.jTextField5.setText(String.valueOf(p.y));
                this.jTextField6.setText("");
                this.jTextField7.setText("");
                this.jButtonShow.doClick();
                       }
                 }
               
                Paint(proxy);
            }
        }
    }//GEN-LAST:event_formMouseClicked

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        // TODO add your handling code here:
        Paint(proxy);
    }//GEN-LAST:event_formComponentShown

    private void jButtonUndoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonUndoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButtonUndoActionPerformed



    @Override
    public void paint(Graphics arg0) {
        super.paint(arg0);
        Paint(proxy);
    }


    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                MatchFrame mf=new MatchFrame();
                mf.setVisible(true);
            }
        });
    }

    public void Paint(Match jogo)
    {
        proxy=jogo;
        tab.DesenhaTabuleiro(GraphicPanel.getGraphics(),GraphicPanel.getWidth(),GraphicPanel.getHeight(), jogo);
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel GraphicPanel;
    private javax.swing.JButton jButtonMover;
    private javax.swing.JButton jButtonShow;
    private javax.swing.JButton jButtonUndo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    public javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    public javax.swing.JRadioButton jRadioButton1;
    public javax.swing.JRadioButton jRadioButton2;
    public javax.swing.JTextField jTextField4;
    public javax.swing.JTextField jTextField5;
    public javax.swing.JTextField jTextField6;
    public javax.swing.JTextField jTextField7;
    // End of variables declaration//GEN-END:variables

    public void setPossibilidades(boolean[][] possibilidades) {
     //   if (tab.State==TabuleiroGrafico.PlayState.CHOOSING_DESTINATION_PLAYER)
            tab.SetPossibilidades(possibilidades);
    }

}
